<?php
/**
 * Description of metier
 *
 * @author Gmax
 */
class modules_cvtheque_metier extends modele {
    public static $_table = 'metier';
    public $type = 'metier';
    
    private $_id = 0;
    private $_nom = "";
    private $_duree = "0";
    
    function __construct($id=null){
        if($id!==null){
            $this->_id = $id;
            $this->_constructObject();
        }
    }
    
    private function _constructObject(){
        $sqlQuery = 'SELECT ';
        $sqlQuery .= 't.* ';
        $sqlQuery .= 'FROM '.self::getTable().' AS t ';
        $sqlQuery .= 'WHERE t.id='.$this->_id.' ';
        $sqlQuery .= 'LIMIT 1';
        $datas = $this->_getOneResult($sqlQuery);
        if($datas){
            $this->_id = $datas['id'];
            $this->_nom =  $this->outputFormat($datas['nom']);
        }
    }
    
    public function setId($id){
      $this->_id = $id;
    }
    public function getId(){
        return $this->_id;
    }
    public function getNom($input=false){
        if($input)return $this->inputFormat($this->_nom);
        else return $this->outputFormat($this->_nom);
    }
    
    public function setDuree($duree=0){
        $this->_duree = $duree;
    }
    public function getDuree(){
        return $this->_duree;
    }
    
    /**
     * renvoi le tableau des propriétés
     * @param boolean $input
     * @return array
     */
    public function getDatas($input=false) {
        $data = array();
        $data['id'] = $this->getId();
        $data['nom'] = $this->getNom($input);
        $data['modele'] = 'metier';
        
        return $data;
    }
    public function setDatas($datas) {
        foreach($datas as $k=>$v){
            if(substr($k, 0, 1) !== '_') $k = '_'.$k;
            if($k != '_id' && property_exists('modules_cvtheque_metier',$k)){
                $this->$k = $v;
            }
        }        
        return true;
    }
    
    public function save(){ 
        $sqlQuery = 'INSERT INTO '.self::getTable().' ';
        $sqlQuery .= '(nom) ';
        $sqlQuery .= 'VALUES (';
        $sqlQuery .= '"'.$this->getNom(true).'" ';
        $sqlQuery .= ')';
        
        $result = bdd_sql::doSql($sqlQuery,false);
        if($result['count']>0){
            $this->setId(bdd_sql::getLastInsertId());
            $retour['success'] = true;
            $retour['datas'] = $this->getDatas();
        }else{
            $retour['error'] = bdd_sql::$LASTERROR;
        }
        return $retour;
    }

    public function update(){
        $sqlQuery = 'UPDATE '.self::getTable().' SET ';
        $sqlQuery .= 'nom="'.$this->getNom(true).'"';
        $sqlQuery .= 'WHERE id='.$this->getId().' LIMIT 1';
        
        bdd_sql::doSql($sqlQuery,false);
        
        return array('success'=>true,'datas'=>$this->getDatas());
    }
    /**
     *
     * @return array
     */
    public function delete() {
        $sqlDelete = 'DELETE FROM '.self::getTable().' WHERE id='.$this->_id;
        bdd_sql::doSql($sqlDelete,false);

        $sqlQuery = 'DELETE FROM '.PWE::getPrefixBdd().'user_metier WHERE metier_id='.$this->_id;
        bdd_sql::doSql($sqlQuery,false);
        
        return array('success'=>true,'datas'=>array());
    }
    
    /* methodes statiques */
    /**
     * retourne le nom de la table prefixé
     * @return string
     */
    public static function getTable(){
        return PWE::getPrefixBdd().self::$_table;
    }
    
    /**
     * renvoi la liste des métiers
     * @return \modules_cvtheque_metier
     */
    public static function getListe(){
        $sqlQuery = 'SELECT * FROM '.self::getTable().' ORDER BY nom';
        $results = bdd_sql::doSql($sqlQuery,true);
        $retour = array();
        if($results['count']>0){
            foreach($results['results'] as $data){
                $permis = new modules_cvtheque_metier($data['id']);
                $retour[] = $permis;
            }
        }
        return $retour;
    }
}
